package com.linjiang.design.singleton.v2;

//单例模式-懒汉式
public class LazySingletonTest {

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Singleton instance = Singleton.getInstance();
                System.out.println(instance);
            }
        });
        thread.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                Singleton instance = Singleton.getInstance();
                System.out.println(instance);
            }
        });
        thread2.start();
    }

}

class Singleton{
    private static volatile Singleton instance;

    public static Singleton getInstance(){
        if (null == instance){
            synchronized (Singleton.class){
                if (null == instance){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
